/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.drakeet.multitype.sample.moreapis;

import me.drakeet.multitype.MultiTypeAdapter;
import me.drakeet.multitype.sample.ResourceTable;
import me.drakeet.multitype.sample.utils.ViewCache;
import me.drakeet.multitype.sample.utils.ViewUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

/**
 * MoreApisPlaygoundSlice
 */
public class MoreApisPlaygoundSlice extends AbilitySlice {
    private static final int MAX_LENGTH = 50;

    private static final String INITIAL_ITEM = "item 0";

    private ComponentContainer rootContainer;

    private List<String> itemsList;

    private ListContainer listContainer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        rootContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_slice_more_apis_playground, null, false);

        if (rootContainer.findComponentById(ResourceTable.Id_list_main) instanceof ListContainer) {
            listContainer = (ListContainer) rootContainer.findComponentById(ResourceTable.Id_list_main);
        }
        super.setUIContent(rootContainer);
    }

    @Override
    public void onActive() {
        super.onActive();
        initializeView();

        if (rootContainer.findComponentById(ResourceTable.Id_btn_add) instanceof Button) {
            Button btnAdd = (Button) rootContainer.findComponentById(ResourceTable.Id_btn_add);
            btnAdd.setBackground(ViewUtil.customElement());
            btnAdd.setClickedListener(listener -> addNewItem());
        }

        if (rootContainer.findComponentById(ResourceTable.Id_btn_clear) instanceof Button) {
            Button btnClear = (Button) rootContainer.findComponentById(ResourceTable.Id_btn_clear);
            btnClear.setBackground(ViewUtil.customElement());
            btnClear.setClickedListener(listener -> clearList());
        }

        if (rootContainer.findComponentById(ResourceTable.Id_btn_remove) instanceof Button) {
            Button btnRemove = (Button) rootContainer.findComponentById(ResourceTable.Id_btn_remove);
            btnRemove.setBackground(ViewUtil.customElement());
            btnRemove.setClickedListener(listener -> removeItem());
        }
    }

    private void addNewItem() {
        if (itemsList.size() > 0) {
            itemsList.add("item " + itemsList.size());
        }
        if (itemsList.size() == 0) {
            itemsList.add(INITIAL_ITEM);
        }
        addItem(itemsList);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * initializeView
     */
    private void initializeView() {
        itemsList = new ArrayList<>();
        for (int pos = 0; pos < MAX_LENGTH; pos++) {
            itemsList.add("Item" + pos);
        }

        listContainer.setItemProvider(new MultiTypeAdapter<String>( itemsList) {
            @Override
            public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) {
                Component result = convertView;
                if (result == null) {
                    result = LayoutScatter.getInstance(MoreApisPlaygoundSlice.this)
                            .parse(ResourceTable.Layout_communicate_with_binder_item, parent, false);
                }

                ViewCache viewCache = (ViewCache) result.getTag();
                if (viewCache == null) {
                    viewCache = new ViewCache(result);
                    result.setTag(viewCache);
                }

                viewCache.getTextView().setText(itemsList.get(position));
                return result;
            }
        });
    }

    /**
     * addItem
     *
     * @param addList String List
     */
    private void addItem(List<String> addList) {
        listContainer.setItemProvider(new MultiTypeAdapter<String>( addList) {
            @Override
            public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) {
                Component result = convertView;
                if (result == null) {
                    result = LayoutScatter.getInstance(MoreApisPlaygoundSlice.this)
                            .parse(ResourceTable.Layout_communicate_with_binder_item, parent, false);
                }

                ViewCache viewCache = (ViewCache) result.getTag();
                if (viewCache == null) {
                    viewCache = new ViewCache(result);
                    result.setTag(viewCache);
                }

                viewCache.getTextView().setText(addList.get(position));
                return result;
            }
        });
    }

    private void clearList() {
        itemsList.clear();
        listContainer.setItemProvider(new MultiTypeAdapter<String>( itemsList) {
            @Override
            public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) {
                Component result = convertView;
                if (result == null) {
                    result = LayoutScatter.getInstance(MoreApisPlaygoundSlice.this)
                            .parse(ResourceTable.Layout_communicate_with_binder_item, parent, false);
                }

                ViewCache viewCache = (ViewCache) result.getTag();
                if (viewCache == null) {
                    viewCache = new ViewCache(result);
                    result.setTag(viewCache);
                }
                return result;
            }
        });
    }

    private void removeItem() {
        if (itemsList.size() > 0) {
            int maxSize = itemsList.size();
            maxSize = maxSize - 1;
            itemsList.remove(maxSize);
            removeItemFromList();
        }
    }

    /**
     * removeItemFromList
     */
    private void removeItemFromList() {
        listContainer.setItemProvider(new MultiTypeAdapter<String>( itemsList) {
            @Override
            public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) {
                Component result = convertView;
                if (result == null) {
                    result = LayoutScatter.getInstance(MoreApisPlaygoundSlice.this)
                            .parse(ResourceTable.Layout_communicate_with_binder_item, parent, false);
                }

                ViewCache viewCache = (ViewCache) result.getTag();
                if (viewCache == null) {
                    viewCache = new ViewCache(result);
                    result.setTag(viewCache);
                }

                viewCache.getTextView().setText(itemsList.get(position));
                return result;
            }
        });
    }
}